package com.qbccn.blog.utils;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.util.Map;

/**
 * @author linke
 * @date 2019-12-22 下午 22:39
 * @description
 */
public class OkhttpUtils {

    public static String post(String url, Map<String, String> params) {
        OkHttpClient client = new OkHttpClient();
        //参数
        FormBody.Builder body = new FormBody.Builder();
        for (String key : params.keySet()) {
            body.add(key, params.get(key)).build();
        }
        RequestBody requestBody = body.build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
